<template>
  <div class="forgot-password-container">
    <div class="forgot-password-wrapper">
      <div class="forgot-password-card">
        <!-- 头部 -->
        <div class="forgot-password-header">
          <div class="logo">
            <img src="/logo.svg" alt="Logo" />
          </div>
          <h2 class="title">忘记密码</h2>
          <p class="subtitle">
            请输入您的邮箱地址，我们将发送重置链接到您的邮箱
          </p>
        </div>

        <!-- 忘记密码表单 -->
        <el-form
          ref="forgotPasswordFormRef"
          :model="forgotPasswordForm"
          :rules="forgotPasswordRules"
          size="large"
          label-position="top"
          class="forgot-password-form"
        >
          <!-- 邮箱 -->
          <el-form-item label="邮箱地址" prop="email">
            <el-input
              v-model="forgotPasswordForm.email"
              placeholder="请输入您的邮箱地址"
              clearable
              :prefix-icon="Message"
              @keyup.enter="handleForgotPassword"
            />
          </el-form-item>

          <!-- 发送重置邮件按钮 -->
          <el-form-item>
            <el-button
              type="primary"
              size="large"
              :loading="sendLoading"
              :disabled="sendLoading"
              class="send-btn"
              @click="handleForgotPassword"
            >
              {{ sendLoading ? "发送中..." : "发送重置邮件" }}
            </el-button>
          </el-form-item>

          <!-- 成功提示 -->
          <div v-if="emailSent" class="success-message">
            <el-icon class="success-icon"><CircleCheck /></el-icon>
            <p class="success-text">
              重置邮件已发送到 <strong>{{ forgotPasswordForm.email }}</strong>
            </p>
            <p class="success-tip">
              请检查您的邮箱（包括垃圾邮件文件夹），点击邮件中的链接重置密码。
              <br />
              重置链接将在24小时内有效。
            </p>
          </div>

          <!-- 返回登录 -->
          <div class="back-to-login">
            <router-link to="/user/login" class="link">
              <el-icon><ArrowLeft /></el-icon>
              返回登录
            </router-link>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { Message, CircleCheck, ArrowLeft } from "@element-plus/icons-vue";
import {
  forgotPasswordApi,
  UserAuthUtils,
  type ForgotPasswordRequest
} from "@/api/user-auth";

defineOptions({
  name: "ForgotPassword"
});

// 表单引用
const forgotPasswordFormRef = ref<FormInstance>();

// 表单数据
const forgotPasswordForm = reactive<ForgotPasswordRequest>({
  email: ""
});

// 状态管理
const sendLoading = ref(false);
const emailSent = ref(false);

// 表单验证规则
const forgotPasswordRules: FormRules = {
  email: [
    { required: true, message: "请输入邮箱地址", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        if (!UserAuthUtils.validateEmail(value)) {
          callback(new Error("邮箱格式不正确"));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ]
};

// 处理忘记密码
const handleForgotPassword = async () => {
  if (!forgotPasswordFormRef.value) return;

  try {
    const valid = await forgotPasswordFormRef.value.validate();
    if (!valid) return;

    sendLoading.value = true;

    const response = await forgotPasswordApi(forgotPasswordForm);

    if (response.code === 200) {
      emailSent.value = true;
      ElMessage.success("重置邮件发送成功");
    } else {
      ElMessage.error(response.message || "发送失败");
    }
  } catch (error) {
    ElMessage.error(UserAuthUtils.formatErrorMessage(error));
  } finally {
    sendLoading.value = false;
  }
};
</script>

<style scoped lang="scss">
// 响应式设计
@media (width <= 640px) {
  .forgot-password-container {
    padding: 16px;
  }

  .forgot-password-card {
    padding: 24px;
  }

  .forgot-password-header {
    margin-bottom: 24px;

    .title {
      font-size: 24px;
    }

    .subtitle {
      font-size: 14px;
    }
  }
}

// 暗色主题适配
@media (prefers-color-scheme: dark) {
  .forgot-password-card {
    color: #f9fafb;
    background: #1f2937;
  }

  .forgot-password-header {
    .title {
      color: #f9fafb;
    }

    .subtitle {
      color: #d1d5db;
    }
  }

  .forgot-password-form {
    :deep(.el-form-item__label) {
      color: #e5e7eb;
    }

    .success-message {
      background: #1e3a8a;
      border-color: #3b82f6;

      .success-text {
        color: #f9fafb;
      }

      .success-tip {
        color: #d1d5db;
      }
    }
  }
}

.forgot-password-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
  background: linear-gradient(135deg, #409eff 0%, #66b1ff 100%);
}

.forgot-password-wrapper {
  width: 100%;
  max-width: 450px;
}

.forgot-password-card {
  padding: 40px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgb(0 0 0 / 10%);
  backdrop-filter: blur(10px);
}

.forgot-password-header {
  margin-bottom: 32px;
  text-align: center;

  .logo {
    margin-bottom: 16px;

    img {
      width: 64px;
      height: 64px;
    }
  }

  .title {
    margin: 0 0 8px;
    font-size: 28px;
    font-weight: 600;
    color: #1f2937;
  }

  .subtitle {
    margin: 0;
    font-size: 16px;
    line-height: 1.5;
    color: #6b7280;
  }
}

.forgot-password-form {
  .el-form-item {
    margin-bottom: 20px;

    :deep(.el-form-item__label) {
      margin-bottom: 8px;
      font-weight: 500;
      color: #374151;
    }

    :deep(.el-input__wrapper) {
      border-radius: 8px;
      box-shadow: 0 1px 3px rgb(0 0 0 / 10%);
      transition: all 0.3s ease;

      &:hover {
        box-shadow: 0 4px 6px rgb(0 0 0 / 10%);
      }

      &.is-focus {
        box-shadow: 0 0 0 2px rgb(64 158 255 / 20%);
      }
    }
  }

  .send-btn {
    width: 100%;
    height: 48px;
    font-size: 16px;
    font-weight: 500;
    background: linear-gradient(135deg, #409eff 0%, #66b1ff 100%);
    border: none;
    border-radius: 8px;
    transition: all 0.3s ease;

    &:hover {
      box-shadow: 0 8px 16px rgb(64 158 255 / 30%);
      transform: translateY(-2px);
    }

    &:active {
      transform: translateY(0);
    }

    &.is-loading,
    &.is-disabled {
      box-shadow: none;
      transform: none;
    }
  }

  .success-message {
    padding: 24px;
    margin-bottom: 24px;
    text-align: center;
    background: #f0f9ff;
    border: 1px solid #bae6fd;
    border-radius: 8px;

    .success-icon {
      margin-bottom: 16px;
      font-size: 48px;
      color: #10b981;
    }

    .success-text {
      margin: 0 0 12px;
      font-size: 16px;
      font-weight: 500;
      color: #1f2937;
    }

    .success-tip {
      margin: 0;
      font-size: 14px;
      line-height: 1.5;
      color: #6b7280;
    }
  }

  .back-to-login {
    margin-top: 24px;
    text-align: center;

    .link {
      display: inline-flex;
      gap: 8px;
      align-items: center;
      font-weight: 500;
      color: #409eff;
      text-decoration: none;
      transition: color 0.3s ease;

      &:hover {
        color: #337ecc;
      }

      .el-icon {
        font-size: 16px;
      }
    }
  }
}
</style>
